//
//  GridLoginViewController.swift
//  IOSAPP
//
//  Created by wondertek on 2024/3/13.
//

import UIKit
import SnapKit
import Toast_Swift

class GridLoginViewController: UIViewController {

    private let loginViewModel: GridLoginViewModel = GridLoginViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = Theme.Color.Background.default
        view.addSubview(topVew)
        view.addSubview(loginInputView)
        view.addSubview(bottomView)
        topVew.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(200)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        loginInputView.snp.makeConstraints { make in
            make.top.equalTo(topVew.snp.bottom).offset(10)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(160)
        }
        
        bottomView.snp.makeConstraints { make in
            make.top.equalTo(loginInputView.snp.bottom).offset(20)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(160)
        }
    }
    
    lazy var topVew: GridTopView = {
        let view = GridTopView()
        view.isUserInteractionEnabled = true
        view.delegate = self
        return view
    }()
   
    lazy var loginInputView: GridLoginInputView = {
        let view = GridLoginInputView()
        view.delegate = self
        return view
    }()

    lazy var bottomView: GridLoginBottomView = {
        let view = GridLoginBottomView()
        view.delegate = self
        return view
    }()
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: --GridTopViewDelegate
extension GridLoginViewController: GridTopViewDelegate {
    
    func gridTopViewChangeLanguage() {
       
        print("gridTopViewChangeLanguage")
        
        let alertController = UIAlertController(title: NSLocalizedString("语言设置", tableName: "GridLocalizable", comment: ""), message: nil, preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: NSLocalizedString("取消", tableName: "GridLocalizable", comment: ""), style: .cancel, handler: nil)
        let deleteAction = UIAlertAction(title: NSLocalizedString("中文", tableName: "GridLocalizable", comment: ""), style: .default) { action in
            if GridLanguageManager.shared.language != .Chinese {
                GridLanguageManager.shared.language = .Chinese
                GridLanguageManager.saveLanguage(chooseLanguage: .Chinese)
            }
        }
         
        let archiveAction = UIAlertAction(title: NSLocalizedString("英文", tableName: "GridLocalizable", comment: ""), style: .default) { action in
            if GridLanguageManager.shared.language != .English {
                GridLanguageManager.shared.language = .English
                GridLanguageManager.saveLanguage(chooseLanguage: .English)
            }
        }
         
        alertController.addAction(cancelAction)
         
        alertController.addAction(deleteAction)
         
        alertController.addAction(archiveAction)
         
        self.present(alertController, animated: true, completion: nil)

    }
}

// MARK: --GridLoginBottomViewDelegate
extension GridLoginViewController: GridLoginBottomViewDelegate {
    
    func gridLoginBottomView(view: GridLoginBottomView, isSelectd: Bool) {
        if view == self.bottomView {
            loginViewModel.isAgree = isSelectd
        }
    }
    
    func gridLoginBottomViewClickLogin(view: GridLoginBottomView) {
        if view == self.bottomView {
            if let message = self.loginViewModel.jundgeIsLogin() {
                self.view.makeToast(message, duration: 3.0, position: .center)
            } else {
                // 登录逻辑处理
                print("登录逻辑处理")
                GridLoginManager.shared.updateToken(token: "TokenNew")
                if UIApplication.shared.delegate != nil {
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.resetRootViewController()
                }
            }
        }
    }
    
}

// MARK: --GridLoginInputViewDelegate
extension GridLoginViewController: GridLoginInputViewDelegate {
    
    func gridLoginInputView(view: GridLoginInputView, nameText: String?, type: GridLoginInptutType) {
        print(nameText ?? "")
        if type == .name {
            self.loginViewModel.userName = nameText ?? ""
        } else {
            self.loginViewModel.userPassword = nameText ?? ""
        }
    }
}
